import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import os

# .csv数据文件目录
directory = "src/build/"

def read_data(file_name):
    # 使用完整的文件路径
    full_path = os.path.join(directory, file_name)
    data = pd.read_csv(full_path, header=None)  # 指定没有列标题
    x = data[0].tolist()  
    y = data[1].tolist()  
    return x, y

# n=2
fig = plt.figure(figsize=(12, 10))
plt.suptitle('Truncated Functions n=2', fontsize=16)

# Plot n=2
axs = fig.subplots(4, 4)
for i in range(4):
    for j in range(i, 4):
        file_name = f"data_F_2_{i}_{j}.csv"
        if os.path.exists(os.path.join(directory, file_name)):
            x, y = read_data(file_name)
            ax = axs[j, i]  # 直接通过行列索引获取子图对象
            ax.plot(x, y, label=f'Knots: {i}, {j}')
            ax.set_title(f'B_{i}^{j}(x)')
            ax.set_xlabel('x')
            ax.set_ylabel('y')
            ax.set_ylim(0, 2)
            ax.grid()

# Adjust layout and save the figure for n=2
plt.tight_layout(rect=[0, 0, 1, 0.95])  
plt.savefig(f'figure/F2.png')  # 确保figure目录存在
plt.close()

# n=1
fig = plt.figure(figsize=(12, 10))
plt.suptitle('Truncated Functions n=1', fontsize=16)

# Plot n=1
axs = fig.subplots(3, 3)
for i in range(3):
    for j in range(i, 3):
        file_name = f"data_F_1_{i}_{j}.csv"
        if os.path.exists(os.path.join(directory, file_name)):
            x, y = read_data(file_name)
            ax = axs[j, i]  # 直接通过行列索引获取子图对象
            ax.plot(x, y, label=f'Knots: {i}, {j}')
            ax.set_title(f'B_{i}^{j}(x)')
            ax.set_xlabel('x')
            ax.set_ylabel('y')
            ax.set_ylim(0, 2)
            ax.grid()

# Adjust layout and save the figure for n=1
plt.tight_layout(rect=[0, 0, 1, 0.95])  # Make room for the title
plt.savefig(f'figure/F1.png')  # 确保figure目录存在
plt.close()